#ifndef BROWSER_H
#define BROWSER_H

#pragma message(__LOC__ "DONT FORGET ABOUT CComModule _Module; in main.cpp!!!")

#include <atlbase.h>
#include <atlwin.h>
#include <exdispid.h>

class Browser
	: public QWidget
	, public IDispatch
	, public IDocHostUIHandler
{
	Q_OBJECT

	CAxWindow axWindow;
	CComPtr<IWebBrowser2> spWebBrowser2;
	CComPtr<IConnectionPoint> spConnectionPoint;
	CComPtr<IHTMLWindow2> spHTMLWindow2;
	CComPtr<IOleClientSite> spClientSite;
	CComPtr<IOleObject> spControl;


	DWORD dwCookie;
	int nagigateCount;

public:
	Browser(QWidget * parent, const char * name = "Browser");
	virtual ~Browser();

	virtual void setFocus();

	void resizeEvent(QResizeEvent * e);
	void goTo(const QString & addr);
	void execJS(const QString & s);

	BSTR QStringToBSTR(const QString &str);

	// IUnknown
	STDMETHOD(QueryInterface)(REFIID iid, void **ppvObject);
	STDMETHODIMP_(ULONG) AddRef() { return 1; }
	STDMETHODIMP_(ULONG) Release() { return 1; }

	// IDispatch
	STDMETHOD(GetTypeInfoCount)(UINT *) { return E_NOTIMPL; }
	STDMETHOD(GetTypeInfo)(UINT, LCID, ITypeInfo **) { return E_NOTIMPL; }
	STDMETHOD(GetIDsOfNames)(REFIID, LPOLESTR *, UINT, LCID, DISPID *);
	STDMETHOD(Invoke)(DISPID, REFIID, LCID, WORD, DISPPARAMS *, VARIANT *, EXCEPINFO *, UINT *);

	// IDocHostUIHandler
	STDMETHOD(ShowContextMenu)(DWORD, POINT FAR *, IUnknown FAR *, IDispatch FAR *) { return S_OK; }
	STDMETHOD(GetHostInfo)(DOCHOSTUIINFO FAR *) { return E_NOTIMPL; }
	STDMETHOD(ShowUI)(DWORD, IOleInPlaceActiveObject FAR *, IOleCommandTarget FAR *,
		IOleInPlaceFrame FAR *, IOleInPlaceUIWindow FAR *) { return E_NOTIMPL; }
	STDMETHOD(HideUI)(void) { return E_NOTIMPL; }
	STDMETHOD(UpdateUI)(void) { return E_NOTIMPL; }
	STDMETHOD(EnableModeless)(BOOL) { return E_NOTIMPL; }
	STDMETHOD(OnDocWindowActivate)(BOOL) { return E_NOTIMPL; }
	STDMETHOD(OnFrameWindowActivate)(BOOL) { return E_NOTIMPL; }
	STDMETHOD(ResizeBorder)(LPCRECT, IOleInPlaceUIWindow FAR *, BOOL) { return E_NOTIMPL; }
	STDMETHOD(TranslateAccelerator)(LPMSG, const GUID FAR *, DWORD) { qDebug("TranslateAccelerator"); return S_OK; /* E_NOTIMPL; */}
	STDMETHOD(GetOptionKeyPath)(LPOLESTR FAR *, DWORD) { return E_NOTIMPL; }
	STDMETHOD(GetDropTarget)(IDropTarget *, IDropTarget **) { return E_NOTIMPL; }
	STDMETHOD(GetExternal)(IDispatch **);
	STDMETHOD(TranslateUrl)(DWORD, OLECHAR *, OLECHAR **) { return E_NOTIMPL; }
	STDMETHOD(FilterDataObject)(IDataObject *, IDataObject **) { return E_NOTIMPL; }

signals:
	void beforeNavigate(const QString & url, bool * ok);
	void navigateComplete2();
	void action(const QString &, const QString &);
};


#endif // BROWSER_H
